//
//  AsyncHttp.swift
//  HttpStydu
//
//  Created by yongkang on 2017/8/11.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import Alamofire
import AlamofireObjectMapper
import SwiftyJSON

class AsyncHttp: NSObject{
    
    
    static let HttpRequest = AsyncHttp()
    
    func getHomeData(page : Int, finished:@escaping (_ newItems: [News]) -> ()){
        
        let url = "http://a.xcar.com.cn/interface/6.0/getNewsList.php"
        
        let params = ["limit": 200,
                      "offset": 0,
                      "type": 2]
        
        Alamofire.request(url, method: .post, parameters: params).responseObject { (response: DataResponse<NewsMode>) in
            
            guard response.result.isSuccess else {
                return
            }
            let newMode = response.result.value
            if let item = newMode?.newsList {
               finished(item)
            }
        }
   }
    
    
    
    
    
    /// 获取单品数据
    func getProductData(finished:@escaping (_ products: [Product]) -> ()) {

        let url = "http://api.dantangapp.com/v2/items"
        let params = ["gender": 1,
                      "generation": 1,
                      "limit": 20,
                      "offset": 0]
        Alamofire.request(url, parameters: params).responseObject { (response: DataResponse<ProductMode>) in
                
            guard response.result.isSuccess else {
                return
            }
            var productList:[Product] = []
            let productMode = response.result.value
            
            if let productItem = productMode?.data?.items{
                for item in productItem {
                    productList.append(item.data!)
                }
            }
            finished(productList)
        }
    }
    
    
        
        
}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

